package com.greate.community.event;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.Event;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
// import org.springframework.kafka.core.KafkaTemplate;
// import org.springframework.stereotype.Component;

/**
 * 事件的生产者
 */
@Component
public class EventProducer implements ApplicationEventPublisherAware, ApplicationContextAware {

    // @Autowired
    // private KafkaTemplate kafkaTemplate;

    private ApplicationContext applicationContext;
    private ApplicationEventPublisher applicationEventPublisher;

    /**
     * 处理事件
     * @param event
     */
    public void fireEvent(Event event) {
        // 将事件发布到指定的主题
        // kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
        applicationEventPublisher.publishEvent(new SendDisEvent(applicationContext,event));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;


    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

}
